// index.js
// 获取应用实例
const app = getApp()
const cityArr=[{
  "id": 1,
  "title": "河南省",
  "parent_id": 0,
  "order": 0,
  "created_at": null,
  "updated_at": null,
  "children": [
    {
      "id": 2,
      "title": "郑州市",
      "parent_id": 1,
      "order": 0,
      "created_at": null,
      "updated_at": null
    },
  ]
}]
Page({
  data: {
    cityName:'郑州市',
    flagsearch:false,//搜索弹窗
    searchkey: '',//搜索关键字
    searchBackcolor: false,//顶部搜索背景色
    showAddess: false,//显示城市选择
    cityList: [{
      values:cityArr,
      className: 'column1',
    },
    {
      values:cityArr[0].children,
      className: 'column2',
      defaultIndex: 1,
    }],
    imgheights: [], //所有banner图片的高度  
    swiperImg: [{ id: 0, img: '../../images/banner.png' }, { id: 0, img: '../../images/banner.png' }],//banner
    current: 0,//当前banner.swiper
    active:0,
    recommend: {menu: {1: "推荐", 2: "新品", 3: "智能", 4: "食品"}},
    list:[
      {id:0,img:"https://yccanteen.oss-cn-beijing.aliyuncs.com/eshop/hot_act/Od35kw7iCHzGZdhh6lP4WsJhW1LIdAzMyCV8Dtr7.png",name:'商品1'},
      {id:0,img:"https://yccanteen.oss-cn-beijing.aliyuncs.com/eshop/hot_act/mm13DSL1zJKd9jyD4kisQzVJfPWJ1rkLk2TlyUto.png",name:'商品2'},
      {id:0,img:"https://yccanteen.oss-cn-beijing.aliyuncs.com/eshop/hot_act/Od35kw7iCHzGZdhh6lP4WsJhW1LIdAzMyCV8Dtr7.png",name:'商品3'},
      {id:0,img:"https://yccanteen.oss-cn-beijing.aliyuncs.com/eshop/hot_act/Od35kw7iCHzGZdhh6lP4WsJhW1LIdAzMyCV8Dtr7.png",name:'商品4'},
      {id:0,img:"https://yceat-20200506.oss-cn-shanghai.aliyuncs.com/goods/cover/93d2593070574bc840ff3468a411262d.png",name:'商品5'},
      {id:0,img:"https://yccanteen.oss-cn-beijing.aliyuncs.com/eshop/hot_act/Od35kw7iCHzGZdhh6lP4WsJhW1LIdAzMyCV8Dtr7.png",name:'商品6'},
      {id:0,img:"https://yccanteen.oss-cn-beijing.aliyuncs.com/eshop/hot_act/Od35kw7iCHzGZdhh6lP4WsJhW1LIdAzMyCV8Dtr7.png",name:'商品7'},
      {id:0,img:"https://yceat-20200506.oss-cn-shanghai.aliyuncs.com/goods/cover/83159b6f24bf069790083c2b3c528a2d.jpg",name:'商品8'},
      {id:0,img:"https://yccanteen.oss-cn-beijing.aliyuncs.com/eshop/hot_act/Od35kw7iCHzGZdhh6lP4WsJhW1LIdAzMyCV8Dtr7.png",name:'商品9'},
    ],
    load:["加载中","加载完成","加载失败"]
  },
  onShow(){
    console.log(this.data.swiperImg[0].img.length)
  },
  onLoad() {
   
  },
  imageLoad: function (e) {//获取图片真实宽度  
    var imgwidth = e.detail.width,
      imgheight = e.detail.height,
      //宽高比  
      ratio = imgwidth / imgheight;
    //计算的高度值  
    var viewHeight = 750 / ratio;
    var imgheight = viewHeight;
    var imgheights = this.data.imgheights;
    //把每一张图片的对应的高度记录到数组里  
    imgheights[e.target.dataset.id] = imgheight;
    this.setData({
      imgheights: imgheights
    })
  },
  bindchange(e) {
    this.setData({ current: e.detail.current })
  },
  slelectAddess(){
    this.setData({
      showAddess:true
    })
  },
  clsoeAddress(){//关闭 城市选择
    this.setData({
      showAddess:false
    })
  },
  onConfirmCity(e){//确认城市选择
    console.log(e.detail.value[1])
    this.setData({
      cityName:e.detail.value[1].title,
      showAddess:false
    })
  },
  onPageScroll(e) {
    let _this = this;
    var query = wx.createSelectorQuery()
    query.select('#navList').boundingClientRect()
    query.selectViewport().scrollOffset()
    query.exec(function (res) {//搜索框背景色
      if (res[0].top < 40) {
        _this.setData({
          "searchBackcolor": true
        })
      } else if (res[0].top > 40) {
        _this.setData({
          "searchBackcolor": false
        })
      }
    })
  },
  searchShow(){
    wx.hideTabBar()
    this.setData({
      flagsearch:true
    })
  },
  onChange(e){
    
    this.setData({
      searchkey: e.detail,
    });
    console.log(this.data.searchkey)
    
  },
  onClickRight(){//点击搜索
    if(!this.data.searchkey||this.data.searchkey.match(/^[ ]*$/)){
      wx.showToast({
        title: '请输入关键字',
        icon:'none'
      })
      return
    }
  },
  clearsearch(){//清空搜索字

  },
  onClickLeft(){//后退首页
    wx:wx.showTabBar()
    this.setData({
      flagsearch:false
    })
  },
  openSortPage(){//商品分类
    wx.navigateTo({
      url: '../sort/index',
    })
  },
  onReachBottom(){
    console.log('到底了')
  }
})
